/**
 *   This file is part of Java Simple Sampler.
 *
 *   Java Simple Sampler is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License.
 *
 *   Java Simple Sampler is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Java Simple Sampler.  If not, see <http://www.gnu.org/licenses/>.
 *
 *   @author David Lapetina	
 */
package net.onirik.mao.sampler.gui;

import javax.swing.table.AbstractTableModel;

import net.onirik.mao.sampler.midi.JSSMidiReceiver;
import net.onirik.mao.sampler.sound.SamplePlayer;

public class JSSTableDataModel extends AbstractTableModel {
	private static final long serialVersionUID = 1L;

	private static final int NOTE_COLUMN = 0;

	private static final int SOUND_COLUMN = 1;

	private final int channel;

	private final JSSMidiReceiver receiver;

	public JSSTableDataModel(final int channel, final JSSMidiReceiver receiver) {
		this.channel = channel;
		this.receiver = receiver;
	}

	public int getColumnCount() {
		return 2;
	}

	public int getRowCount() {
		return 129;
	}

	public String getColumnName(int i) {
		switch (i) {
		case 0:
			return "note";
		case 1:
			return "sound";
		default:
			return "";
		}
	}

	public Object getValueAt(int row, int col) {
		switch (col) {
		case NOTE_COLUMN: {
			if (row == 0)
				return "default";
			return Integer.toString(row - 1);
		}
		case SOUND_COLUMN: {
			SamplePlayer player;
			if (row == 0) {
				player = receiver.getSamplePlayerMap(this.channel)
						.getDefaultPlayer();
			} else {
				// -2, note number 1 is id 0 and first row is default
				player = receiver.getSamplePlayerMap(this.channel)
						.getSamplePlayer(row - 2, false);
			}
			if (player != null)
				return player.getResourceName();
			else
				return "";
		}
		default:
			return "";
		}
	}

	public boolean isCellEditable(int rowIndex, int columnIndex) {
		// only sounds is editable;
		return columnIndex == SOUND_COLUMN;
	}
}
